package com.evil.common.application.enums.form;

import com.evil.common.core.enums.RCodeEnum;
import com.evil.common.core.exception.BusinessException;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.Optional;
import java.util.function.Consumer;

/**
 * 控件权限类型
 *
 * @author liyang
 * @date 2022-08-08 00:00
 */
@AllArgsConstructor
public enum ControlPermissionSourceTypeEnum {

    /**
     * 控件权限类型
     */
    PROCESS_NODE(1, "流程节点控件权限"),
    REPORTING(2, "报表控件权限"),
    ;

    @Getter
    private final int id;
    @Getter
    private final String name;

    public static Optional<ControlPermissionSourceTypeEnum> findById(Integer id) {
        if (null == id) {
            return Optional.empty();
        }
        return Arrays.stream(values()).filter(e -> e.id == id).findFirst();
    }

    public static ControlPermissionSourceTypeEnum isExistById(Integer id) {
        return ControlPermissionSourceTypeEnum.findById(id).orElseThrow(ControlPermissionSourceTypeEnum::getBusinessException);
    }

    public static void forEach(Consumer<ControlPermissionSourceTypeEnum> consumer) {
        Arrays.asList(values()).forEach(consumer);
    }

    public static BusinessException getBusinessException() {
        return new BusinessException(RCodeEnum.ERROR_PARAMETER, "控件权限类型参数错误");
    }
}
